package com.cmsky.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cmsky.dao.CmsNodeDAO;
import com.cmsky.model.CmsNode;
import com.cmsky.service.third.CmsQueryService;
import com.cmsky.utils.JsonUtils;

/**
 * 	节点管理相关的控制方法
 * @author jandyfish
 * 
 */
@Controller
@RequestMapping("/m/node")
public class NodeManageController {
	
	private static String className = NodeManageController.class.getName();
	private static Logger nodeManageControllerLogger = Logger.getLogger(className);
	
	@Autowired
	private CmsQueryService cmsQueryService;
	
	@Autowired
	private CmsNodeDAO cmsNodeDAO;
	
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,Model model){
		ModelAndView view = new ModelAndView("/m/node/index");
		return view;
	}
	
	@RequestMapping("/getAllChild/{pid}")
	public @ResponseBody String getAll(HttpServletRequest request,HttpServletResponse response,@PathVariable Integer pid){
		String result = null;
		List<CmsNode> cmsNodeList = cmsQueryService.findNodesByPid(pid, false);
		if(cmsNodeList != null){
			result = JsonUtils.jsonFromObject(cmsNodeList);
		}
		nodeManageControllerLogger.info("<|>"+className+"<|>getAllChild<|>"+result+"<|>");
		return result;
	}
	
	@RequestMapping("/toAdd/{pid}")
	public ModelAndView toAdd(HttpServletRequest request,HttpServletResponse response,Model model){
		ModelAndView view = new ModelAndView("/m/node/add");
		return view;
	}
	
	@RequestMapping("/add/{pid}")
	public @ResponseBody Integer add(HttpServletRequest request,HttpServletResponse response){
		String json = request.getParameter("node");
		CmsNode cmsNode = JsonUtils.cmsNodeFromJson(json);
		int result = cmsNodeDAO.insert(cmsNode);
		nodeManageControllerLogger.info("<|>"+className+"<|>add<|>"+json+"<|>"+result+"<|>");
		return result;
	}
	
	
}
